//
//  ViewController.swift
//  剂查查
//
//  Created by shoubin tao on 2024/11/30.
//  Copyright © 2024 shipin. All rights reserved.
//

import CommonTools

class JCCRegisterViewController: BaseViewController,UITextFieldDelegate {
    @IBAction func goBack(_ sender: Any) {
        navigationController?.popViewController(animated: true)
    }
    
    @IBAction func sendCodeAction(_ sender: Any) {
//        EZLoadingActivity.show("正在获取验证码...", disableUI: true)
        guard let phone = phoneTextField.text else {
            toast(message: "请输入手机号")
            return
        }
        if phone.validate(.phoneNum) {
            toast(message: "请输入正确的手机号")
        }
        showLoading()
        JCCNet.sendPhoneCode(phone: phone, completion: {model,error  in
            hideLoading()
            toast(message: model?.info ?? "")
        })
    }
    @IBOutlet weak var registerBgView: UIView!
    
    @IBOutlet weak var phoneTextField: UITextField!
    
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var codeTextField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.registerBgView.clipsToBounds = true
        self.registerBgView.layer.cornerRadius = 30
        self.phoneTextField.delegate = self
        self.passwordTextField.delegate = self
        self.codeTextField.delegate = self
        setTitle("登陆注册")
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            // 将text field的内容添加到你的数据模型或者用途中
            // ...
     
            // 使用这个方法来退出键盘
            textField.resignFirstResponder()
            return true
        }
    @IBAction func registerAction(_ sender: Any) {
        let phoneNo = phoneTextField.text ?? ""
        let password = passwordTextField.text ?? ""
        let authCode = codeTextField.text ?? ""
        if !phoneNo.validate(.phoneNum)  {
            toast(message: "请输入正确手机号")
            return
        }
        if password.count < 6  {
            toast(message: "请输入大于6位密码")
            return
        }
        if !authCode.validate(.authCode) {
            toast(message: "请输入正确验证码")
            return
        }
        showLoading(title: "正在注册账号...")
        JCCNet.register(phone: phoneNo, password: password, verify: authCode,completion: {model,error  in
            hideLoading()
            toast(message: model?.info ?? "")
            if let dict = model?.result as? [String:Any] {
                let authorization:String? = dict["token"] as? String
                if (authorization != ""){
                    UserDefaults.standard.set(authorization, forKey: "token")
                    if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "JCCScanTipViewController") {
                        self.navigationController?.pushViewController(viewController, animated: true)
                    }
                }
            }
        })
    }
    
    @IBAction func jcc_showPrivacy(_ sender: UIButton) {
        showPrivacy(type: sender.tag == 0 ? .privacy : .user)
    }
}

